<?php
if (!isset($_SESSION)) {
    session_start();
}
class shop extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    protected $configs;

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('smarty3', '', 'smarty');
        $this->load->model('pushclimb/product_model');
        $this->load->model('pushclimb/package_model');
        $this->load->model('admin-planners/setting_model');
        $this->load->model('pushclimb/type_model');
        include APPPATH . 'libraries/defu.php';
        $this->setting_model=new setting_model();
        $this->product_model=new product_model();
        $this->package_model=new package_model();
        $this->type_model= new type_model();
    }

    function Init() {
        $data = $this->setting_model->gets();
        foreach ($data as $v) {
            $_SESSION["SETTINGS"][$v->Key] = $v->Value;
        }
    }

    public function index() {
        $Data=null;
        if(isset($_SESSION["Discounts"])){
            $Data["Discounts"]=$_SESSION["Discounts"];
        }else{
            $dis=$this->setting_model->getByKey("discounts");
            $dis=$dis[0];
            if($dis->Update!=date("Y-m-d")){
                $v=$this->product_model->getRand10();
                if(isset($v[0])){
                    $params=array(
                        "Value"=>$v[0]->dis
                    );
                    $this->setting_model->update($dis->ID,$params);
                    $dis=$v[0]->dis;
                }
            }else{
                $dis=$dis->Value;
            }
            $_SESSION["Discounts"]=$dis;
        }
        
        $tmp = $this->setting_model->getByKey("mad-rock-products");
        $Data["mad-rock-products"]= $tmp[0];
        
        $tmp = $this->setting_model->getByKey("how-to-buy");
        $Data["how-to-buy"]= $tmp[0];
        
        
        $Data["Categorys"]=$this->type_model->getByType("category");
        $Data["CategoryIndex"]=$Data["Categorys"][0]->_alias;
        $obj = $this->product_model->gets(0,$Data["Categorys"][0]->_id);
        $Data["Packages"]=$this->package_model->gets();
        $Data["PageIndex"]=0;
        if (count($obj) > 0)
            $Data["Products"] = $obj;
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/04_shop");
    }
    public function category($ca="",$page=0) {
        $Data=null;
        if(isset($_SESSION["Discounts"])){
            $Data["Discounts"]=$_SESSION["Discounts"];
        }else{
            $dis=$this->setting_model->getByKey("discounts");
            $dis=$dis[0];
            if($dis->Update!=date("Y-m-d")){
                $v=$this->product_model->getRand10();
                if(isset($v[0])){
                    $params=array(
                        "Value"=>$v[0]->dis
                    );
                    $this->setting_model->update($dis->ID,$params);
                    $dis=$v[0]->dis;
                }
            }else{
                $dis=$dis->Value;
            }
            $_SESSION["Discounts"]=$dis;
        }
        $tmp = $this->setting_model->getByKey("mad-rock-products");
        $Data["mad-rock-products"]= $tmp[0];
        
        $tmp = $this->setting_model->getByKey("how-to-buy");
        $Data["how-to-buy"]= $tmp[0];
        
        $Data["CategoryIndex"]=$ca;
        $Data["Categorys"]=$this->type_model->getByType("category");
        foreach($Data["Categorys"] as $c){
            if($c->_alias==$ca){
                break;
            }
        }
        
        $obj = $this->product_model->gets($page,$c->_id);
        $Data["Packages"]=$this->package_model->gets();
        $Data["PageIndex"]=$page;
        if (count($obj) > 0)
            $Data["Products"] = $obj;
        
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/04_shop");
    }
    function addtocart($alias){
        
        $pr=$this->product_model->getByAlias($alias);
        if(isset($pr[0])){
            if(!isset($_SESSION["CART"]["PR"][$pr[0]->Alias])){
                $_SESSION["CART"]["PR"][$pr[0]->Alias]=$pr[0];
                $_SESSION["CART"]["PR"][$pr[0]->Alias]->NUM=1;
                $code = 1;
                $msg = "Added <tt>".$pr[0]->ProductName . "</tt> to your cart!";
            }else{
                $_SESSION["CART"]["PR"][$pr[0]->Alias]->NUM++;
                $code = 1;
                $msg = "Added <tt>".$pr[0]->ProductName . "</tt> to your cart!!";
            }
        }else{
            $code = -1;
            $msg = "Fail.Product is not exits. Please try again.";
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }
    function removetocart($alias){
        
        if(isset($_SESSION["CART"]["PR"][$alias])){
            
            $code = 1;
            $msg = "Removed <tt>".$_SESSION["CART"]["PR"][$alias]->ProductName . "</tt> from your cart!";
            unset($_SESSION["CART"]["PR"][$alias]);
        }else{
            $code = -1;
            $msg = "Product is not exist";
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }
    function addpktocart($alias){
        
        $pk=$this->package_model->getByAlias($alias);
        if(isset($pk[0])){
            if(!isset($_SESSION["CART"]["PK"][$pk[0]->Alias])){
                $_SESSION["CART"]["PK"][$pk[0]->Alias]=$pk[0];
                $_SESSION["CART"]["PK"][$pk[0]->Alias]->NUM=1;
                $code = 1;
                $msg = "Added <tt>".$pk[0]->PackageName . "</tt> to your cart!";
            }else{
                $_SESSION["CART"]["PR"][$pk[0]->Alias]->NUM++;
                $code = 1;
                $msg = "Added <tt>".$pk[0]->PackageName . "</tt> to your cart!!";
            }
        }else{
            $code = -1;
            $msg = "Fail.Package is not exits. Please try again.";
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }
    function removepktocart($alias){
        
        if(isset($_SESSION["CART"]["PK"][$alias])){
            
            $code = 1;
            $msg = "Removed <tt>".$_SESSION["CART"]["PK"][$alias]->PackageName . "</tt> from your cart!";
            unset($_SESSION["CART"]["PK"][$alias]);
        }else{
            $code = -1;
            $msg = "Product is not exist";
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }
    function reloadyourcart(){
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->display("pushclimbing/pages/12_cart");
    }
}
